<?php
header("Content-type: image/png");
$img_width = 200;
$img_height = 200;

if(isset($_GET['size']))
{
	$img_width = $_GET['size'];
	$img_height = $_GET['size'];
}

$values = $_GET['values'];	// Collects values from the $_GET command. Usage when including image: <img src="image.php?values=1,2,3" />
if(isset($_GET['metric']))
{
	$metric = $_GET['metric'];	// Collects weight metric from the $_GET command. Usage when including image: <img src="image.php?values=1,2,3&metric=kg" />
}
else
{
	$metric = "";
}

$im     = imagecreate($img_width, $img_height);
$white = imagecolorallocate ($im,255,255,255);
$black = imagecolorallocate ($im,0,0,0);
$red = imagecolorallocate ($im, 255, 0, 0);
$blue = imagecolorallocate ($im,0,0,255);
$orange = imagecolorallocate($im, 220, 210, 60);
$bar[0] = imagecolorallocate ($im,0,0,255);
$bar[1] = imagecolorallocate ($im,30,30,255);
$bar[2] = imagecolorallocate ($im,60,60,255);
$bar[3] = imagecolorallocate ($im,90,90,255);
$bar[4] = imagecolorallocate ($im,120,120,255);
$bar[5] = imagecolorallocate ($im,150,150,255);

$vals = explode(",", $values);	// Split the values into an array-list
$count = count($vals);
$barwidth = (($img_width - 40) / $count) - 2; // Calculates the width of each bars depending on how many bars should be drawn

$max = max($vals);	// The highest value of all
if($max == 0)
{
	$max = 1;
}
$interval = $max / 4;	// Intervall between the values on x-axis
$dist = ($img_height - 40) / 4;	// Distance in pixels of the values on x-axis

$x = 35; // Start pos for X
$y = $img_height - 20; // Start pos for Y

foreach($vals as $k => $v)
{
	$barheight = ($v/$max)*($img_height-40); // Calculates height for current bar
	$x1 = $x;
	$x2 = $x+$barwidth - 2;
	$y1 = $y - $barheight;
	$y2 = $y;
	
	$width = floor($barwidth / 5);
	//imagefilledrectangle($im, $x1, $y1, $x2, $y2, $bar[0]);	// Draws the graph
	
	/**
	* Following creates a bar with fading colors
	**/
	if($barwidth > 6)
	{
		for($i=0; $i < 6; $i++)
		{
			imagefilledrectangle($im, $x1 + (($width*$i)/2), $y1, $x2 - (($width*$i)/2), $y2, $bar[$i]);
		}
	}
	
	$xt = $x + ($barwidth - 4) / 2;
	$xt = $xt - ((strlen($v)*2) / 2);
	
	imagestring($im, 1, $xt, $y1 - 15, $v, $red); // Prints the value on top of graph
	imagestring($im, 1, $xt, $y+2, $k+1, $black); // Prints the set number below
	$x += $barwidth + 2; // Set new pos for X
} 
imageline($im, 30, 20, 30, $img_height-18, $black);	// Draws the line to x-axis
imageline($im, 30, $img_height-20, $img_width, $img_height-20, $black); // Draws the line to y-axis

$y = 15; // Start pos for Y
for($i = $max; $i >= 0; $i-=$interval)
{
	imagestring($im, 1, 2, $y, $i, $black);	// Prints the value on x-axis
	imageline($im, 28, $y+5, 32, $y+5, $black); // Draws a line to represent the value
	$y += $dist;
}
imagestring($im, 1, 15, 5, $metric, $black);
imagestring($im, 1, $img_width-25, $img_height-10, "sets", $black);

imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $black);	// Draws the border of the image
imagepng($im);
imagedestroy($im);
?>